package ru.usu.gv.uicomponents.viewitems.vertex;

import com.sun.j3d.utils.geometry.Sphere;

import ru.usu.gv.core.GraphCanvas;
import ru.usu.gv.styles.StyleFacade;
import ru.usu.gv.uicomponents.UIElementBase;
import ru.usu.gv.uicomponents.viewitems.ViewItemBase;
import ru.usu.gv.utils.geometry.Polyline;

import javax.media.j3d.Appearance;
import javax.media.j3d.DistanceLOD;
import javax.media.j3d.Switch;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;

import java.awt.Color;

/**
 @author Sergey Pupyrev 
 Apr 2, 2007
 
 TODO: memory leak!
 */

public class LODSphereView extends ViewItemBase implements IVertexViewItem
{
	private Switch sw;
	private DistanceLOD lod;
	private Sphere one, two, three, four;

	public LODSphereView(Appearance appearance, TransformGroup tg)
	{
		super(tg, appearance, appearance.getMaterial());

		sw = new Switch(0);
		sw.setCapability(javax.media.j3d.Switch.ALLOW_SWITCH_READ);
		sw.setCapability(javax.media.j3d.Switch.ALLOW_SWITCH_WRITE);

		float normalSize = (float) StyleFacade.getVertexSize();
		one = new Sphere(normalSize, Sphere.GENERATE_NORMALS, 40, appearance);
		two = new Sphere(normalSize, Sphere.GENERATE_NORMALS, 20, appearance);
		three = new Sphere(normalSize, Sphere.GENERATE_NORMALS, 10, appearance);
		four = new Sphere(normalSize, Sphere.GENERATE_NORMALS, 5, appearance);
		sw.addChild(one);
		sw.addChild(two);
		sw.addChild(three);
		sw.addChild(four);

		// set up the DistanceLOD behavior
		lod = new DistanceLOD(new float[] { 20f, 40f, 80f });
		lod.addSwitch(sw);
		lod.setSchedulingBounds(GraphCanvas.getInstance().getBoundingSphere());

		tg.addChild(sw);
		tg.addChild(lod);
	}

	public void makePickable(UIElementBase ge)
	{
		ge.makePickable(one.getShape());
		ge.makePickable(two.getShape());
		ge.makePickable(three.getShape());
		ge.makePickable(four.getShape());
	}

	@Override
	public void setColor(Color color)
	{
		super.setColor(color);
		getMaterial().setDiffuseColor(new Color3f(color));
	}

	public Polyline getBoundingPolygon()
	{
		// TODO Auto-generated method stub
		return null;
	}

}
